﻿using ASP;
using HKB.Common;
using HKB.Web;
using HKB.Web.Shop;
using System;
using System.Data;
using System.Text;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using ucApi;

public partial class Passport_Login : Shop, IRequiresSessionState
{
    public string LoginUserName;
    public string LoginPassword;
    public string UCUserName;
    public string UCPassword;
    public int CookiesTime = 0;
    public string m_Other = "";
    public string Opwd = "";
    private string LoginErrString = "";
    private int isOutWebSite = 0;
    private string m_ComeUrl = "";
    protected DefaultProfile profile
    {
        get
        {
            return (DefaultProfile)this.Context.Profile;
        }
    }
    protected global_asax applicationInstance
    {
        get
        {
            return (global_asax)this.Context.ApplicationInstance;
        }
    }
    public int GetLoginTimes
    {
        get
        {
            int result;
            if (Fetch.GetCookies("LoginTimes") == string.Empty)
            {
                base.Response.Cookies["LoginTimes"].Value = "0";
                int num = this.config["LoginInterval"].ToInt32(new int[0]);
                if (num > 0)
                {
                    base.Response.Cookies["LoginTimes"].Expires = DateTime.Now.AddMinutes((double)num);
                }
                result = 0;
            }
            else
            {
                result = Fetch.GetCookies("LoginTimes").ToInt32(new int[0]);
            }
            return result;
        }
        set
        {
            base.Response.Cookies["LoginTimes"].Value = value.IsNull(new object[0]);
        }
    }
    protected void Page_Init(object sender, EventArgs e)
    {
        this.SelectLoginWay();
    }
    protected void SelectLoginWay()
    {
        this.LoginUserName = Fetch.Post("UserName");
        this.LoginPassword = Fetch.Post("UserPassword");
        this.Opwd = Fetch.Post("UserPassword");
        if (this.LoginUserName != string.Empty && this.LoginPassword != string.Empty)
        {
            this.UCUserName = this.LoginUserName;
            this.UCPassword = this.LoginPassword;
            this.isOutWebSite = 1;
            this.InitOutLogin();
            base.Response.End();
        }
        if (base.user.CheckUserLogin())
        {
            this.m_ComeUrl = Text.GetComeUrl(Fetch.GetOrPost("ComeUrl"));
            if (this.m_ComeUrl.Trim() == "")
            {
                this.m_ComeUrl = "/Index.aspx";
            }
            try
            {
                base.Response.Redirect(this.m_ComeUrl, true);
            }
            catch (Exception ex)
            {
                base.Response.Redirect(this.m_ComeUrl, true);
            }
        }
        this.LoginUserName = Fetch.GetOrPost(H_UserName.ClientID);
        this.LoginPassword = Fetch.GetOrPost("H_UserPassword");
        this.Opwd = Fetch.GetOrPost("H_UserPassword");
        if (this.LoginUserName != string.Empty && this.LoginPassword != string.Empty)
        {
            this.UCUserName = this.LoginUserName;
            this.UCPassword = this.LoginPassword;
            this.InitLogin(false);
            base.Response.End();
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!base.IsPostBack)
        {
            if (base.Request["mlname"] != null && base.Request["mlpassword"] != null)
            {
                this.LoginUserName = base.Request["mlname"].Trim();
                this.LoginPassword = base.Request["mlpassword"].Trim();
                this.InitLogin(true);
                base.Response.End();
            }
            this.H_UserPassword.Attributes.Add("onkeydown", "keydown();");
            this.H_UserName.Attributes.Add("onmouseover", "this.select()");
            //if (this.config["EnableLoginCheckCode"].IsNull(new object[0]) == "1")
            //{
            //    this.show_checkcode.Visible = true;
            //}
            //else
            //{
            //    this.show_checkcode.Visible = false;
            //}
            this.m_ComeUrl = Text.GetComeUrl(Fetch.GetOrPost("ComeUrl"));
            if (this.m_ComeUrl != string.Empty)
            {
                this.LoginErrString = Language.Get("PassPort/notLogin");
            }
            this.H_ComeUrl.Value = this.m_ComeUrl;
            if (base.Request.UrlReferrer != null)
            {
                base.Response.Cookies["lastRequest"].Value = base.Request.UrlReferrer.AbsoluteUri;
                base.Response.Cookies["lastRequest"].Expires = DateTime.Now.AddDays(10.0);
            }
        }
    }
    protected void InitLogin(bool nocheckCode)
    {
        if (this.LoginUserName == "")
        {
            this.WriteErrMsg("", Language.Get("PassPort/UserNameNull"), "");
        }
        if (this.LoginPassword == "")
        {
            this.WriteErrMsg("", Language.Get("PassPort/PasswordNull"), "");
        }
        if (!nocheckCode && this.config["EnableLoginCheckCode"].ToString() == "1")
        {
            string text = Fetch.Post("CheckCode");
            if (text == string.Empty)
            {
                this.WriteErrMsg("", Language.Get("PassPort/CheckCodeErr"), "");
            }
            if (!this.CheckValidateCode(text))
            {
                this.WriteErrMsg("", Language.Get("PassPort/CheckCodeErr"), "");
            }
        }
        this.LoginPassword = this.Md5(this.LoginPassword.Trim(), 32);
        this.m_ComeUrl = Text.GetComeUrl(Fetch.Post("H_ComeUrl"));
        this.CheckLogin();
    }
    protected void InitOutLogin()
    {
        if (this.config["HLIntegration"].IsNull(new object[0]) != "1")
        {
            this.WriteErrMsg("", Language.Get("Common/PoassPortNotStart"), "");
        }
        string orPost = Fetch.GetOrPost("Code");
        Encoding pagecode = Encoding.GetEncoding("gb2312");
        if (orPost != string.Empty)
        {
            if (orPost.ToLower() == "utf-8" || orPost.ToLower() == "utf8")
            {
                pagecode = Encoding.UTF8;
            }
        }
        this.LoginUserName = Fetch.GetOrPost("UserName", pagecode);
        this.LoginPassword = Fetch.GetOrPost("UserPassword");
        this.Opwd = Fetch.GetOrPost("UserPassword");
        if (this.LoginPassword != string.Empty)
        {
            this.LoginPassword = this.Md5(this.LoginPassword, 32);
        }
        if (Fetch.GetOrPost("CookieDate") != string.Empty)
        {
            this.CookiesTime = Fetch.GetOrPost("CookieDate").ToInt32(new int[0]);
        }
        this.m_ComeUrl = Text.GetComeUrl(Fetch.GetOrPost("ComeUrl"));
        this.m_Other = Fetch.GetOrPost("Other");
        this.CheckLogin();
    }
    public void CheckLogin()
    {
        int num = 0;
        if (this.config["TryLoginTimes"].IsNull(new object[0]) != "")
        {
            try
            {
                num = this.config["TryLoginTimes"].ToInt32(new int[0]);
            }
            catch
            {
            }
        }
        int num2 = 0;
        try
        {
            num2 = this.config["LoginInterval"].ToInt32(new int[0]);
        }
        catch
        {
        }
        string text = this.config["LoginIPBan"].IsNull(new object[0]);
        string text2 = this.config["LoginIPAccess"].IsNull(new object[0]);
        string comeUrl = this.config["SiteUrl"].IsNull(new object[0]);
        string userHostAddress = this.Page.Request.UserHostAddress;
        string text3 = DateTime.Now.IsNull(new object[0]);
        string a = this.config["GuestBookShowIP"].IsNull(new object[0]);
        if (!string.IsNullOrEmpty(text2))
        {
            if (!text2.Contains(this.GetIP()))
            {
                this.WriteErrMsg("", Language.Get("PassPort/IPIsLocked"), "");
            }
        }
        if (!string.IsNullOrEmpty(text))
        {
            if (text.Contains(this.GetIP()))
            {
                this.WriteErrMsg("", Language.Get("PassPort/IPIsLocked"), "");
            }
        }
        if (num > 0)
        {
            if (this.GetLoginTimes >= num)
            {
                if (this.isOutWebSite == 1)
                {
                    this.WriteErrMsg("", Language.Get("PassPort/LoginTimesErr", num2.ToString()), "Login.aspx");
                }
                else
                {
                    this.WriteErrMsg("", Language.Get("PassPort/LoginTimesErr", num2.ToString()), "");
                }
            }
        }
        base.user.UserName = this.LoginUserName;
        base.user.Password = this.LoginPassword;
        int num3 = this.config["RegPresent1"].ToInt32(new int[0]);
        int num4 = this.config["RegPresent2"].ToInt32(new int[0]);
        int value = this.config["RegPresent3"].ToInt32(new int[0]);
        int userEP = this.config["RegPresent4"].ToInt32(new int[0]);
        UserBll userBll = new UserBll();
        AppSetings appSetings = new AppSetings();
        if (appSetings.GetConfig("UC_ONorOFF") != null)
        {
            if (appSetings.GetConfig("UC_ONorOFF") == "ON")
            {
                RTN_UserLogin rTN_UserLogin = Func.uc_user_login(this.UCUserName, this.UCPassword);
                if (rTN_UserLogin.Uid == -1)
                {
                    DataTable dataTable = userBll.GetUserByUserName(this.UCUserName);
                    if (dataTable.Rows.Count > 0)
                    {
                        userBll.Delete(Convert.ToInt32(dataTable.Rows[0]["UserID"]));
                    }
                    this.WriteErrMsg("", "用户不存在，或者被删除", "");
                }
                else
                {
                    if (rTN_UserLogin.Uid == -2)
                    {
                        this.WriteErrMsg("", "密码错", "");
                    }
                    else
                    {
                        if (rTN_UserLogin.Uid == -3)
                        {
                            this.WriteErrMsg("", "安全提问错", "");
                        }
                        else
                        {
                            if (rTN_UserLogin.Uid == -4)
                            {
                                this.WriteErrMsg("", "用户或密码为空", "");
                            }
                            else
                            {
                                DataTable dataTable = userBll.GetUserByUserID(rTN_UserLogin.Uid);
                                if (dataTable.Rows.Count <= 0)
                                {
                                    userBll.AddUser(rTN_UserLogin.Uid, base.user.UserName, base.user.Password, rTN_UserLogin.Email, "", "", 1, 1, userEP, 0, value, Fetch.UserIp);
                                }
                                else
                                {
                                    userBll.UpdatePassport(rTN_UserLogin.Uid, base.user.Password, rTN_UserLogin.Email, dataTable.Rows[0]["Question"].ToString(), dataTable.Rows[0]["Answer"].ToString());
                                }
                                string s = Func.uc_user_synlogin(rTN_UserLogin.Uid);
                                base.Response.Write(s);
                            }
                        }
                    }
                }
            }
        }
        if (!base.user.UserLogin())
        {
            if (num > 0)
            {
                this.GetLoginTimes++;
            }
            this.WriteErrMsg("", base.user.ErrMsg, "");
        }
        if (this.m_ComeUrl == string.Empty)
        {
            this.m_ComeUrl = comeUrl;
        }
        if (this.config["HLIntegration"].ToString() == "1" && a != "1")
        {
            new Integrate(this.conn)
            {
                ComeUrl = this.m_ComeUrl
            }.Send("Login", this.LoginUserName, this.LoginPassword, "", "", "", this.CookiesTime, this.Opwd, this.m_Other);
        }
        else
        {
            this.m_ComeUrl = Text.GetComeUrl(Fetch.GetOrPost("ComeUrl"));
            if (this.m_ComeUrl.Trim() == "")
            {
                this.m_ComeUrl = "/Index.aspx";
            }
            try
            {
                base.Response.Redirect(this.m_ComeUrl, true);
            }
            catch (Exception)
            {
                base.Response.Redirect(this.m_ComeUrl, true);
            }
        }
    }
    public string GetIP()
    {
        return Fetch.UserIp;
    }
}
